<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">



<title>
  
    Archive: 2019/1
  
</title>

<meta name="description" content="I am a current master at UPENN. This page serves as a record of my life. I may post study blogs, daily blogs, and personal opions. XD">
<meta property="og:type" content="website">
<meta property="og:title" content="Zexi &quot;Josh&quot; Jin">
<meta property="og:url" content="http://yoursite.com/archives/2019/01/index.html">
<meta property="og:site_name" content="Zexi &quot;Josh&quot; Jin">
<meta property="og:description" content="I am a current master at UPENN. This page serves as a record of my life. I may post study blogs, daily blogs, and personal opions. XD">
<meta property="og:locale" content="en">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Zexi &quot;Josh&quot; Jin">
<meta name="twitter:description" content="I am a current master at UPENN. This page serves as a record of my life. I may post study blogs, daily blogs, and personal opions. XD">


  <link rel="alternative" href="/atom.xml" title="Zexi &#34;Josh&#34; Jin" type="application/atom+xml">



  <link rel="icon" href="/favicon.png">


<link rel="stylesheet" href="/perfect-scrollbar/css/perfect-scrollbar.min.css">
<link rel="stylesheet" href="/styles/main.css"><!-- hexo-inject:begin --><!-- hexo-inject:end -->






</head>
<body>
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="mobile-header">
  <button class="sidebar-toggle" type="button">
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
  <a class="title" href="/">Zexi &#34;Josh&#34; Jin</a>
</div>

  <div class="main-container">
    <div class="sidebar">
  <div class="header">
    <h1 class="title"><a href="/">Zexi &#34;Josh&#34; Jin</a></h1>
    
    <div class="info">
      <div class="content">
        
          <div class="description">I am a current master at UPENN. This page serves as a record of my life. I may post study blogs, daily blogs, and personal opions. XD</div>
        
        
      </div>
      
        <div class="avatar">
          
            <a href="/about"><img src="https://i.ibb.co/XXV2FLb/Screen-Shot-2019-10-17-at-12-31-49-PM.png"></a>
          
        </div>
      
    </div>
  </div>
  <div class="body">
    
      
        <ul class="nav">
          
            
              <li class="category-list-container">
                <a href="javascript:;">Category</a>
                <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/backend/">backend</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/data-analysis/">data analysis</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/data-structure/">data structure</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/photos/">photos</a><span class="category-list-count">2</span></li></ul>
              </li>
            
          
            
              <li class="tag-list-container">
                <a href="javascript:;">Tag</a>
                <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/LeetCode/">LeetCode</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/algorithm/">algorithm</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/backend/">backend</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/backtrack/">backtrack</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/binary-tree/">binary tree</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/concurrency/">concurrency</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/django/">django</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/new-york/">new york</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/pandas/">pandas</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/photos/">photos</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/">python</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/roc-auc/">roc-auc</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sorting/">sorting</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/travel/">travel</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/traversal/">traversal</a><span class="tag-list-count">1</span></li></ul>
              </li>
            
          
            
              <li class="archive-list-container">
                <a href="javascript:;">Archive</a>
                <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/">2019</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/">2018</a><span class="archive-list-count">1</span></li></ul>
              </li>
            
          
        </ul>
      
        <ul class="nav">
          
            
              <li>
                <a href="/" title="Homepage">Homepage</a>
              </li>
            
          
            
              <li>
                <a href="/about" title="About">About</a>
              </li>
            
          
        </ul>
      
    
  </div>
</div>

    <div class="main-content">
      
        <div style="max-width: 1000px">
      
          

  
  
    
      
      
      <section class="archives-wrap">
        <div class="archive-year-wrap">
          <h1><a href="/archives/2019" class="archive-year">2019</a></h1>
        </div>
        <div class="post-list">
    
  

    <div class="post-list-item article">
      <h3 class="article-header">
        <a href="/2019/01/20/Django-basic-1/">
  Django Basics, Part 1
</a>

      </h3>
      

      <div class="article-info">
        <a href="/2019/01/20/Django-basic-1/"><span class="article-date">
  2019-01-20
</span>
</a>
        
	<span class="article-category tagcloud">
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/backend/">backend</a></li></ul>
	</span>


        
	<span class="article-tag tagcloud">
		<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/backend/">backend</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/django/">django</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/">python</a></li></ul>
	</span>


      </div>
      <div class="article-entry">
        
          <h2 id="Django-Directory-Levels"><a href="#Django-Directory-Levels" class="headerlink" title="Django Directory Levels"></a>Django Directory Levels</h2><ol>
<li>manage.py: we use this to communicate with Django server</li>
<li>project directory:<ol>
<li>__init__.py: tells Django to use directory as a python package</li>
<li>settings.py: Django configuration file</li>
<li>urls.py: url schemaa</li>
<li>wsgi.py: project and wsgi integration web server</li>
</ol>
</li>
</ol>
<h2 id="Database-MySQL-config"><a href="#Database-MySQL-config" class="headerlink" title="Database (MySQL) config"></a>Database (MySQL) config</h2><ul>
<li>dependencies used:<br>  mysql                     5.7.24               h56848d4_0<br>  mysql-connector-c         6.1.11               hccea1a4_0<br>  mysql-connector-python    8.0.17           py27h3febbb0_0    anaconda<br>  mysql-python              1.2.5            py27h1de35cc_0    anaconda<br>  pymysql                   0.9.3                    py27_0  </li>
<li><p>__init__.py, add the following code: </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">import pymysql</span><br><span class="line">pymysql.install_as_MySQLdb()</span><br></pre></td></tr></table></figure>
</li>
<li><p>in settings.py: add database configurations to connect to the database</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">DATABASES = {</span><br><span class="line">    &apos;default&apos;: {</span><br><span class="line">        &apos;ENGINE&apos;: &apos;django.db.backends.mysql&apos;,</span><br><span class="line">        &apos;NAME&apos;: # server name</span><br><span class="line">        &apos;USER&apos;: # user name</span><br><span class="line">        &apos;PASSWORD&apos;: # password</span><br><span class="line">        &apos;HOST&apos;: &apos;localhost&apos;,</span><br><span class="line">        &apos;PORT&apos;: &apos;3306&apos;,</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="new-application"><a href="#new-application" class="headerlink" title="new application"></a>new application</h2><ul>
<li><p>init new application:  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python manage.py startapp &lt;myApp&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>codes above creates:</p>
<ul>
<li>admin.py: website config</li>
<li>models.py: model</li>
<li>views.py: view</li>
</ul>
</li>
<li>In setting, add <myapp> to INSTALLED_APP:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">INSTALLED_APPS = [</span><br><span class="line">    &apos;django.contrib.admin&apos;,</span><br><span class="line">    &apos;django.contrib.auth&apos;,</span><br><span class="line">    &apos;django.contrib.contenttypes&apos;,</span><br><span class="line">    &apos;django.contrib.sessions&apos;,</span><br><span class="line">    &apos;django.contrib.messages&apos;,</span><br><span class="line">    &apos;django.contrib.staticfiles&apos;,</span><br><span class="line">    &apos;myApp&apos;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
</myapp></li>
</ul>
<h2 id="models-py-file"><a href="#models-py-file" class="headerlink" title="models.py file"></a>models.py file</h2><ul>
<li><p>example models.py can be as the following:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line">from __future__ import unicode_literals</span><br><span class="line">from django.db import models</span><br><span class="line"></span><br><span class="line"># Create your models here.</span><br><span class="line">class Grades(models.Model):</span><br><span class="line">    gname = models.CharField(max_length=20)</span><br><span class="line">    gdate = models.DateTimeField()</span><br><span class="line">    ggirlnum = models.IntegerField()</span><br><span class="line">    gboynum = models.IntegerField()</span><br><span class="line">    isDelete = models.BooleanField(default=False)</span><br><span class="line">    </span><br><span class="line">    def __str__(self):</span><br><span class="line">        return &quot;%s-%d-%d&quot;%(self.gname, self.gboynum, self.ggrilnum)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class Students(models.Model):</span><br><span class="line">    sname = models.CharField(max_length=20)</span><br><span class="line">    sgender = models.BooleanField(default=True)</span><br><span class="line">    sage = models.IntegerField()</span><br><span class="line">    scontend = models.CharField(max_length=20)</span><br><span class="line">    isDelete = models.BooleanField(default=False)</span><br><span class="line">    sgrade = models.ForeignKey(&quot;Grades&quot;)</span><br><span class="line">    </span><br><span class="line">    def __str__(self):</span><br><span class="line">            return &quot;%s-%d-%s&quot;%(self.sname, self.sage, self.scontend)</span><br></pre></td></tr></table></figure>
</li>
<li><p>make migration and execute migration file: will create database according to models.py</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">python manage.py makemigrations # create migration file</span><br><span class="line">python manage.py migrate        # execute migration file</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="To-add-new-entries-with-manage-py-shell"><a href="#To-add-new-entries-with-manage-py-shell" class="headerlink" title="To add new entries with manage.py shell"></a>To add new entries with manage.py shell</h2><ul>
<li><p>enter shell:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python manage.py shell</span><br></pre></td></tr></table></figure>
</li>
<li><p>import packages in shell:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">from myApp.models import Grades, Students</span><br><span class="line">from django.utils import timezone</span><br><span class="line">from datetime import *</span><br></pre></td></tr></table></figure>
</li>
<li><p>add new entry to db:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">grade1 = Grades()</span><br><span class="line">grade1.gname = &quot;something&quot;</span><br><span class="line">grade1.gdate = datetime(year=,month=,day=)</span><br><span class="line">grade1.ggirlnum = 70</span><br><span class="line">grade1.gboynum = 35</span><br><span class="line">grade1.save()</span><br></pre></td></tr></table></figure>
</li>
<li><p>some other command:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Grades.objects.all()    # get all data entries in Grades</span><br><span class="line">g = Grades.objects.get(pk=1)    # get the 1st object in Grades</span><br><span class="line">g.gboynum = 45</span><br><span class="line">g.save()    # alternate existing data</span><br><span class="line">d.delete()  # delete, including the one in db</span><br><span class="line">stu1 = g.students_set.create(sname=...)     # add new student with one line</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="run-server"><a href="#run-server" class="headerlink" title="run server:"></a>run server:</h2><ul>
<li><p>execute:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python manage.py runserver ip:port</span><br></pre></td></tr></table></figure>
</li>
<li><p>Admin:</p>
<ul>
<li>publish content</li>
<li>add/modify/delete content</li>
<li>add &#x201C;&#x2018;django.contrib.admin&#x2019;&#x201D; in INSTALLED_APPS</li>
<li>exists by defualt</li>
<li>add &#x201C;/admin&#x201D; and log in</li>
<li>change language or timezone based on preference</li>
</ul>
</li>
</ul>
<p>(to be continued&#x2026;)</p>

        
      </div>
    </div>

  






          <div class="main-footer">
  
    © 2019 Zexi &#34;Josh&#34; Jin - Powered by <a href="http://hexo.io" target="_blank">Hexo</a> - Theme <a href="https://github.com/denjones/hexo-theme-chan" target="_blank">Chan</a>
  
</div>

      
        </div>
      
    </section></div>
  </div>
  <script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

  <link rel="stylesheet" href="/PhotoSwipe/photoswipe.css">
  <link rel="stylesheet" href="/PhotoSwipe/default-skin/default-skin.css">

  <!-- Root element of PhotoSwipe. Must have class pswp. -->
  <div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe.
             It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

      <!-- Container that holds slides.
                PhotoSwipe keeps only 3 of them in the DOM to save memory.
                Don't modify these 3 pswp__item elements, data is added later on. -->
      <div class="pswp__container">
        <div class="pswp__item"></div>
        <div class="pswp__item"></div>
        <div class="pswp__item"></div>
      </div>

      <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
      <div class="pswp__ui pswp__ui--hidden">

        <div class="pswp__top-bar">

          <!--  Controls are self-explanatory. Order can be changed. -->

          <div class="pswp__counter"></div>

          <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

          <button class="pswp__button pswp__button--share" title="Share"></button>

          <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

          <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

          <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
          <!-- element will get class pswp__preloader--active when preloader is running -->
          <div class="pswp__preloader">
            <div class="pswp__preloader__icn">
              <div class="pswp__preloader__cut">
                <div class="pswp__preloader__donut"></div>
              </div>
            </div>
          </div>
        </div>

        <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
          <div class="pswp__share-tooltip"></div>
        </div>

        <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)"></button>

        <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)"></button>

        <div class="pswp__caption">
          <div class="pswp__caption__center"></div>
        </div>
      </div>
    </div>
  </div>

  <script src="/PhotoSwipe/photoswipe.js"></script>
  <script src="/PhotoSwipe/photoswipe-ui-default.js"></script>


<script src="/perfect-scrollbar/js/min/perfect-scrollbar.min.js"></script>
<script src="/scripts/main.js"></script>

</div><!-- hexo-inject:begin --><!-- hexo-inject:end --></body>
</html>
